import numpy as np

from tools.general_tools import GeneralTools


class Trajectory:

    def __init__(self):
        self.real_point_trajectory = np.array([-1, -1])
        self.trajectory_index = -1
        self.point_number = 0
        self.after_interp_point_number = 0

        # define a list to store index of cells and subcells that points are in in the trajectory
        self.trajectory_level1_cell_index_array = np.array([], dtype=int)
        self.trajectory_level2_index_sequence = np.array([], dtype=int)
        self.level2_unique_index_sequence = np.array([], dtype=int)
        self.after_interp_level2_unique_index_sequence = np.array([], dtype=int)
        self.experiment_array = np.array([], dtype=int)


    # this function get point number of trajectory
    def get_single_trajectory_point_number(self):
        if self.point_number == 0:
            self.point_number = self.real_point_trajectory.shape[0]
            if self.point_number == 0:
                raise ValueError('Point number of trajectory {} is zero!'.format(self.trajectory_index))
        return self.point_number

    def give_regularized_trajectory_cell_density(self, cell_number):
        level1_index_array = self.trajectory_level1_cell_index_array
        general_tool1 = GeneralTools()
        try:
            raw_frequency = general_tool1.density_of_single_array(cell_number, level1_index_array)
        except IndexError:
            raise IndexError('trajectory {} has wrong cell index'.format(self.trajectory_index))
        # 返回一个大小为全体网格数量的数组，其中数组值表示这条轨迹在这个网格出现的频率
        point_number = self.get_single_trajectory_point_number()
        regularized_frequency = raw_frequency / point_number
        return regularized_frequency  # 返回归一化后的频率



    def give_unique_trajectory(self):
        gt1 = GeneralTools()
        level2_cell_index_array = self.trajectory_level2_index_sequence
        unrepeated_index_sequence, frequency_array = gt1.unrepeated_int_array(level2_cell_index_array)
        self.level2_unique_index_sequence = unrepeated_index_sequence




